import type { ConsultOrderItem, FollowType } from '@/types/consult.d'
import {
  cancelOrder,
  deleteOrder,
  followOrUnfollow,
  getPrescriptionPic
} from '@/services/consult'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  showToast,
  type FormInstance
} from 'vant'
import { OrderType } from '@/enum'
import { getMedicalOrderDetail } from '@/services/order'
import type { OrderDetail } from '@/types/order'
import { onMounted, onUnmounted, ref, type Ref } from 'vue'
import { sendMobileCode } from '@/services/user'
import type { CodeType } from '@/types/user'

/**
 * 关注与取消关注
 * @param type
 * @returns
 */
export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followOrUnfollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }

  return { loading, follow }
}

/**
 * 查看处方
 */
export const useShowPrescription = () => {
  const onShowPrescription = async (id?: string) => {
    if (!id) return
    const res = await getPrescriptionPic(id)
    return showImagePreview([res.data.url])
  }
  return { onShowPrescription }
}

/**
 * 取消订单
 */
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, cancelConsultOrder }
}

/**
 * 删除订单
 * @param cb  回调函数
 * @returns
 */
export const useDeleteOrder = (cb: () => void) => {
  const deleteLoading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      deleteLoading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      // 此处处理的业务不同
      cb && cb()
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      deleteLoading.value = false
    }
  }
  return { deleteLoading, deleteConsultOrder }
}

/**
 * 根据订单的id获取订单的详情
 * @param id
 * @returns
 */
export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  const time = ref(0)
  const form = ref<FormInstance>()
  let timerId: number
  const sendCode = async () => {
    // 2.1 如果 倒计时>0 不能倒计时
    if (time.value > 0) return
    // 2.2 校验手机号
    await form.value?.validate('mobile')
    // 2.3 获取短信验证码
    const res = await sendMobileCode(mobile.value, type)
    showToast({
      message: res.data.code,
      duration: 5 * 1000
    })
    // 2.4 开始倒计时
    time.value = 60
    window.clearInterval(timerId)
    timerId = window.setInterval(() => {
      time.value--
      if (time.value <= 0) window.clearInterval(timerId)
    }, 1000)
  }
  onUnmounted(() => {
    window.clearInterval(timerId)
  })
  return { time, form, sendCode }
}
